﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Workflow.Project
{
    public partial class MainTemp : System.Web.UI.Page
    {
        private ProjectMainManager objProjectMainManager = new ProjectMainManager();
        private WorkflowManager objWorkflowManager = new WorkflowManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager();
        int workParentId;
        protected void Page_Load(object sender, EventArgs e)
        {
             workParentId = Convert.ToInt32(Request.QueryString["WorkParentId"]);
             if (!IsPostBack)
             {
                 try
                 {
                     
                     ProjectMain objProjectMian = objProjectMainManager.GetProjectInfo(workParentId, null, "PJMainsTemp");
                     Informaction(objProjectMian);
                 }
                 catch (Exception ex)
                 {
                     string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                     Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                 }
             }
        }
        private string ShowDate(string id, string date, string strTemp)
        {
            string str = "";
            if (date != "1900-01-01")
            {
                str = strTemp + string.Format("document.getElementById('{0}').value = '{1}';", id, date);
                //Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            return str;

        }
        void Informaction(ProjectMain objProjectMian)
        {
            string strTemp = "";
            if (objProjectMian != null)
            {
                txtPJNo.Text = objProjectMian.PJId;
                txtPJName.Text = objProjectMian.PJName;
                txtMold.Text = objProjectMian.Mold;
                txtMaktx.Text = objProjectMian.Maktx;
                txtMaktxName.Text = objProjectMian.MaktxName;
                txtCustomer.Text = objProjectMian.Customer;
                txtBusiness.Text = objProjectMian.BusinessPlate;
                txtPM.Text = objProjectMian.PM;
                txtReason.Text = objProjectMian.Reason;
                ddlPjType.SelectedValue = objProjectMian.PJType;
                string[] strs = objProjectMian.MoldType.Split(',');
                for (int i = 0; i < strs.Length; i++)
                {
                    if (strs[i] == "量产模") { cblMoldType.Items[0].Selected = true; continue; }
                    if (strs[i] == "样品模") { cblMoldType.Items[1].Selected = true; continue; }
                    if (strs[i] == "商品模") { cblMoldType.Items[2].Selected = true; continue; }
                    if (strs[i] == "转移模") { cblMoldType.Items[3].Selected = true; continue; }
                    if (strs[i] == "改模") { cblMoldType.Items[4].Selected = true; continue; }
                    if (strs[i] == "备模") { cblMoldType.Items[5].Selected = true; continue; }
                }
                txtData.Text = objProjectMian.Statues == "1" ? "已同步" : "未同步";
                string startDate = objProjectMian.StartDate.ToString("yyyy-MM-dd");
                strTemp = ShowDate("txtStartDate", startDate, strTemp);
                string t1EndDate = objProjectMian.T1EndDate.ToString("yyyy-MM-dd");
                strTemp = ShowDate("txtT1EndDate", t1EndDate, strTemp);
                string t2EndDate = objProjectMian.T2EndDate.ToString("yyyy-MM-dd");
                strTemp = ShowDate("txtT2EndDate", t2EndDate, strTemp);
                string t3EndDate = objProjectMian.T3EndDate.ToString("yyyy-MM-dd");
                strTemp = ShowDate("txtT3EndDate", t3EndDate, strTemp);
                string t4EndDate = objProjectMian.T4EndDate.ToString("yyyy-MM-dd");
                strTemp = ShowDate("txtT4EndDate", t4EndDate, strTemp);
                string t5EndDate = objProjectMian.T5EndDate.ToString("yyyy-MM-dd");
                strTemp = ShowDate("txtT5EndDate", t5EndDate, strTemp);
                strTemp = string.Format("<script>{0}</script>", strTemp);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", strTemp);
                txtPJNo.Enabled = false;
                Search.Visible = false;
                int pjCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 6, 7);
                int statusNo = objWorkflowManager.GetWorkflowStatues(workParentId);
                if (pjCount > 0 && statusNo < 1)
                {
                    txtPJName.Enabled = true;
                    txtMold.Enabled = true;
                    txtMaktx.Enabled = true;
                    txtMaktxName.Enabled = true;
                    txtCustomer.Enabled = true;
                    txtBusiness.Enabled = true;
                    txtPM.Enabled = true;
                    ddlPjType.Enabled = true;
                    cblMoldType.Enabled = true;
                    BtnSubmit1.Visible = true;
                    txtReason.Enabled = true;
                }
                else if ((pjCount > 0 && statusNo == 2))
                {
                    BtnSubmit2.Visible = true;
                }
            }
            else
            {
                txtPJNo.Text = "";
                txtPJName.Text = "";
                txtMold.Text = "";
                txtMaktx.Text = "";
                txtMaktxName.Text = "";
                txtCustomer.Text = "";
                txtBusiness.Text = "";
                txtPM.Text = "";
                ddlPjType.SelectedIndex = -1;
                cblMoldType.SelectedIndex = -1;
                txtData.Text = "";
                txtReason.Text = "";
                strTemp = ShowDate("txtStartDate", "", strTemp);
                strTemp = ShowDate("txtT1EndDate", "", strTemp);
                strTemp = ShowDate("txtT2EndDate", "", strTemp);
                strTemp = ShowDate("txtT3EndDate", "", strTemp);
                strTemp = ShowDate("txtT4EndDate", "", strTemp);
                strTemp = ShowDate("txtT5EndDate", "", strTemp);
            }
        }

        protected void BtnSubmit1_Click(object sender, EventArgs e)
        {
            string moldType = "";
            for (int i = 0; i < cblMoldType.Items.Count; i++)
            {
                if (cblMoldType.Items[i].Selected)
                {
                    if (moldType.Length == 0)
                    { moldType = cblMoldType.Items[i].Text; }
                    else { moldType = moldType + "," + cblMoldType.Items[i].Text; }
                }
            }
            ProjectMain objProject = new ProjectMain
            {
                WorkParentId = workParentId,
                PJId = txtPJNo.Text.ToString(),
                PJName = txtPJName.Text.ToString().Trim(),
                Mold = txtMold.Text.ToString().Trim().ToUpper(),
                Maktx = txtMaktx.Text.ToString().Trim(),
                MaktxName = txtMaktxName.Text.ToString().Trim(),
                Customer = txtCustomer.Text.ToString().Trim(),
                BusinessPlate = txtBusiness.Text.ToString().Trim(),
                PM = txtPM.Text.ToString().Trim(),
                PJType = ddlPjType.SelectedValue.ToString(),
                MoldType = moldType,
                Reason = txtReason.Text.ToString().Trim()
            };
            objProject.StartDate = Request.Params.Get("txtStartDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtStartDate"));
            objProject.T1EndDate = Request.Params.Get("txtT1EndDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtT1EndDate"));
            objProject.T2EndDate = Request.Params.Get("txtT2EndDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtT2EndDate"));
            objProject.T3EndDate = Request.Params.Get("txtT3EndDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtT3EndDate"));
            objProject.T4EndDate = Request.Params.Get("txtT4EndDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtT4EndDate"));
            objProject.T5EndDate = Request.Params.Get("txtT5EndDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtT5EndDate"));
            try
            {
                string pjId = objProjectMainManager.InsertOrUpdateProjectInfo(objProject, "PJMainsTemp");
                string msg = string.Format("<script>alert('项目编号:{0}保存成功！')</script>", pjId);
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        protected void BtnSubmit2_Click(object sender, EventArgs e)
        {
            try
            {
                objProjectMainManager.UpdateProjectInfo(workParentId, "1", "PJMainsTemp");
                ProjectMain objProjectMian = objProjectMainManager.GetProjectInfo(workParentId, null, "PJMainsTemp");
                objProjectMainManager.UpdateProjectInfo(objProjectMian, "PJMains", objProjectMian.PJId);
                string msg = string.Format("<script>alert('同步成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
        

        protected void Search_Click(object sender, EventArgs e)
        {
            try
            {
                ProjectMain objProjectMian = objProjectMainManager.GetProjectInfo(-1, txtPJNo.Text.Trim().ToUpper(), "PJMainsTemp");
                string temp = txtPJNo.Text.Trim().ToUpper();
                Informaction(objProjectMian);
                if(objProjectMian==null)
                {
                    Search.Visible = true;
                    string msg = string.Format("<script>alert('没有找到{0}')</script>", temp);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }
                else
                {
                    //判断有没有权限修改
                    if (Session["FullName"].ToString() != objProjectMainManager.GetUserName(txtPJNo.Text.Trim().ToUpper()))
                    {
                        BtnSubmit1.Visible = false;
                        string msg = string.Format("<script>alert('没有权限修改')</script>");
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
    }
}